home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / demo / GDS_COMPLEX.lha / complex / elves.c < prev    next >
C/C++ Source or Header  |  1994-03-15  |  8KB  |  328 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5.  
  6. #include <exec/memory.h>
  7. #include <exec/types.h>
  8. #include <graphics/gfxbase.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/graphics_protos.h>
  12.  
  13. #include "GameSmith:include/libraries/libraries.h"
  14. #include "GameSmith:include/libraries/libptrs.h"
  15. #include "GameSmith:GameSmith.h"
  16.  
  17. #include "elf.h"
  18.  
  19. /*-------------------------------------------------------------------------*/
  20. /* Function Prototypes                                                                       */
  21.  
  22. void parser(int,char **);
  23. int setup(void);
  24. void move_image(void);
  25. void check_bounds(void);
  26. int check_close(void);
  27. void cleanup(void);
  28. void free_arrays(void);
  29.  
  30. /*-------------------------------------------------------------------------*/
  31.  
  32. int elf_cnt,swidth=320,sheight=200,smode=0;
  33. int *x=NULL,*y=NULL,*speedx=NULL,*speedy=NULL;
  34. int dlist=-1;
  35.  
  36. struct anim_cplx *elf;
  37.  
  38. struct display_struct *display;
  39.  
  40. /*-------------------------------------------------------------------------*/
  41.  
  42. main(argc,argv)
  43. int argc;
  44. char *argv[];
  45.  
  46. {
  47.     int err,end=0;
  48.  
  49.     if (argc < 2)
  50.         {
  51.         printf("\nUSAGE: Elves [number of elves] [HIRES] [LACE]\n");
  52.         exit(01);
  53.         }
  54.     if (gs_open_libs(DOS|GRAPHICS,0))
  55.         exit(02);                    /* if can't open libs, abort */
  56.     parser(argc,argv);            /* parse command line args */
  57.     if (err=setup())                /* if couldn't get set up... abort program */
  58.         {
  59.         printf("\nsetup error %d\n",err);
  60.         gs_close_libs();            /* close all libraries */
  61.         exit(03);
  62.         }
  63.     Forbid();
  64.     while (!end)
  65.         {
  66.         move_image();
  67.         end=check_close();        /* end when user hits left mouse */
  68.         }
  69.     Permit();
  70.     cleanup();                        /* close & deallocate everything */
  71.     gs_close_libs();                /* close all libraries */
  72. }
  73.  
  74. /***************************************************************************/
  75.  
  76. void parser(argc,argv)
  77. int argc;
  78. char *argv[];
  79.  
  80. {
  81.     elf_cnt=atoi(argv[1]);        /* number of complexes running around screen */
  82.     if (argc >= 3)
  83.         {
  84.         if (!(stricmp(argv[2],"HIRES")))    /* check for hires spec */
  85.             {
  86.             swidth=640;
  87.             sheight=400;
  88.             if (GfxBase->LibNode.lib_Version >= 36)
  89.                 {
  90.                 if (ModeNotAvailable(DBLNTSCHIRESFF_KEY))
  91.                     smode=HIRES|LACE;
  92.                 else
  93.                     smode=DBLNTSCHIRESFF_KEY;
  94.                 }
  95.             else
  96.                 smode=HIRES|LACE;
  97.             }
  98.         else if (!(stricmp(argv[2],"SUPER")))    /* check for superhires */
  99.             {
  100.             if (GfxBase->LibNode.lib_Version >= 36)
  101.                 {
  102.                 if (ModeNotAvailable(SUPER72_MONITOR_ID | SUPERLACE_KEY))
  103.                     {
  104.                     swidth=640;
  105.                     sheight=400;
  106.                     smode=HIRES|LACE;
  107.                     }
  108.                 else
  109.                     {
  110.                     smode=SUPER72_MONITOR_ID | SUPERLACE_KEY;
  111.                     swidth=800;
  112.                     sheight=600;
  113.                     }
  114.                 }
  115.             else
  116.                 {
  117.                 swidth=640;
  118.                 sheight=400;
  119.                 smode=HIRES|LACE;
  120.                 }
  121.             }
  122.         }
  123. }
  124.  
  125. /***************************************************************************/
  126.  
  127. setup()
  128.  
  129. {
  130.     int cnt,seq,depth=0;
  131.     struct anim_struct *anim;
  132.     struct blit_struct *img;
  133.  
  134.     struct anim_load_struct load =
  135.         {
  136.         "elf",                                /* name of anim file to load */
  137.         NULL,                                    /* ptr to anim upon return */
  138.         NULL,                                    /* ptr to attachment array specification upon return */
  139.         NULL,                                    /* ptr to color map upon return */
  140.         0,                                        /* # entries in the color map */
  141.         8,                                        /* # bits per color map entry */
  142.         0,                                        /* type (filled). 0 = anim, 1 = complex */
  143.         1,                                        /* # elfs to allocate in array */
  144.         0                                        /* flags */
  145.         };
  146.  
  147.     if (!(x=(int *)malloc(elf_cnt*sizeof(int))))
  148.         return(-1);
  149.     if (!(y=(int *)malloc(elf_cnt*sizeof(int))))
  150.         {
  151.         free_arrays();
  152.         return(-1);
  153.         }
  154.     if (!(speedx=(int *)malloc(elf_cnt*sizeof(int))))
  155.         {
  156.         free_arrays();
  157.         return(-1);
  158.         }
  159.     if (!(speedy=(int *)malloc(elf_cnt*sizeof(int))))
  160.         {
  161.         free_arrays();
  162.         return(-1);
  163.         }
  164.     load.array_elements=elf_cnt;        /* user specified # elves */
  165.     if (gs_load_anim(&load))
  166.         {
  167.         free_arrays();
  168.         return(-1);
  169.         }
  170.     elf=load.anim_ptr.cplx;                /* ptr to elf complex */
  171.     if (load.type != 1)                    /* make sure it's an anim complex */
  172.         {
  173.         free_arrays();
  174.         if (load.type == 0)
  175.             gs_free_anim((struct anim_struct *)elf,elf_cnt);
  176.         return(-2);
  177.         }
  178.     anim = elf[0].list;                    /* ptr to 1st anim in complex */
  179.     while (anim)
  180.         {
  181.         img = anim->list;                    /* ptr to 1st image in anim sequence */
  182.         while (img)                            /* find max depth of anim */
  183.             {
  184.             if (img->depth > depth)
  185.                 depth = img->depth;
  186.             if (img->next == img)        /* avoid infinite loop */
  187.                 img=NULL;
  188.             else
  189.                 img=img->next;
  190.             }
  191.         anim=anim->cplx_next;            /* next anim in complex */
  192.         }
  193.     if (!(display=gs_display(swidth,sheight,depth,smode,GSV_DOUBLE,load.cmap)))
  194.         {
  195.         free_arrays();
  196.         FreeMem(load.cmap,load.cmap_entries*sizeof(long));
  197.         gs_free_cplx(elf,elf_cnt);
  198.         return(-3);
  199.         }
  200.     FreeMem(load.cmap,load.cmap_entries*sizeof(long));
  201.     if ((dlist=gs_get_display_list()) < 0)
  202.         {
  203.         cleanup();
  204.         return(-4);
  205.         }
  206.     gs_init_anim(dlist,display->vp->bitmap1,display->vp->bitmap2);
  207.     gs_set_anim_bounds(dlist,0,0,swidth-1,sheight-1);
  208.     gs_random(0);                                /* seed random function */
  209.     for (cnt=0; cnt < elf_cnt; cnt++)
  210.         {
  211.         x[cnt] = gs_random(swidth);            /* random X,Y coords */
  212.         y[cnt] = gs_random(sheight);
  213.         while ((speedx[cnt] = gs_random(11)) < 3);
  214.         while ((speedy[cnt] = gs_random(4)) == 0);
  215.         if (cnt&1)
  216.             {
  217.             speedx[cnt]=-speedx[cnt];
  218.             speedy[cnt]=-speedy[cnt];
  219.             seq=ELF_LEFT;
  220.             }
  221.         else
  222.             seq=ELF_RIGHT;
  223.         if (gs_add_anim_cplx(dlist,&elf[cnt],x[cnt],y[cnt],seq,elf[cnt].list->prio))
  224.             {
  225.             cleanup();                        /* release everything */
  226.             return(-5);                        /* return failure */
  227.             }
  228.         }
  229.     gs_draw_anims(dlist);
  230.     check_bounds();
  231.     gs_next_anim_page(dlist);
  232.     gs_show_display(display,1);
  233.     gs_flip_display(display,1);
  234.     return(0);
  235. }
  236.  
  237. /***************************************************************************/
  238.  
  239. void move_image()
  240.  
  241. /* move and animate the graphic objects on the screen */
  242.  
  243. {
  244.     int cnt;
  245.  
  246.     for (cnt=0; cnt < elf_cnt; cnt++)
  247.         {
  248.         x[cnt]+=speedx[cnt];
  249.         y[cnt]+=speedy[cnt];
  250.         gs_anim_cplx(&elf[cnt],x[cnt],y[cnt]);
  251.         }
  252.     while (display->flags & GSV_FLIP);    /* while display not flipped */
  253.     gs_draw_anims(dlist);                    /* draw them anim objects! */
  254.     check_bounds();                        /* keep a watch on them pesky keeblers */
  255.     gs_next_anim_page(dlist);            /* tell anim sys to use other bitmap */
  256.     gs_flip_display(display,1);        /* switch to other display, sync */
  257. }
  258.  
  259. /***************************************************************************/
  260.  
  261. void check_bounds()
  262.  
  263. {
  264.     int cnt;
  265.  
  266.     for (cnt=0; cnt < elf_cnt; cnt++)
  267.         {
  268.         if (elf[cnt].anim->flags & ANIM_BOUNDS_X1)
  269.             {
  270.             x[cnt]=elf[cnt].anim->x;
  271.             speedx[cnt]=-speedx[cnt];
  272.             gs_set_cplx_seq(&elf[cnt],ELF_RIGHT,x[cnt],y[cnt]);
  273.             }
  274.         else if (elf[cnt].anim->flags & ANIM_BOUNDS_X2)
  275.             {
  276.             x[cnt]=elf[cnt].anim->x;
  277.             speedx[cnt]=-speedx[cnt];
  278.             gs_set_cplx_seq(&elf[cnt],ELF_LEFT,x[cnt],y[cnt]);
  279.             }
  280.         if (elf[cnt].anim->flags & (ANIM_BOUNDS_Y1|ANIM_BOUNDS_Y2))
  281.             {
  282.             y[cnt]=elf[cnt].anim->y;
  283.             speedy[cnt]=-speedy[cnt];
  284.             }
  285.         }
  286. }
  287.  
  288. /***************************************************************************/
  289.  
  290. int check_close()
  291.  
  292. /* check for user input */
  293.  
  294. {
  295.     if (gs_joystick(0) & JOY_BUTTON1)    /* IF (mouse button pressed) */
  296.         return(1);                            /* ..time to wrap up */
  297.     return(0);                                /* ELSE (keep going) */
  298. }
  299.  
  300. /***************************************************************************/
  301.  
  302. void cleanup()
  303.  
  304. /* release all resources and memory */
  305.  
  306. {
  307.     free_arrays();
  308.     gs_free_cplx(elf,elf_cnt);
  309.     if (dlist >= 0)
  310.         gs_free_display_list(dlist);
  311.     gs_remove_display(display);
  312. }
  313.  
  314. /***************************************************************************/
  315.  
  316. void free_arrays()
  317.  
  318. {
  319.     if (x)
  320.         free(x);                            /* free up control arrays */
  321.     if (y)
  322.         free(y);
  323.     if (speedx)
  324.         free(speedx);
  325.     if (speedy)
  326.         free(speedy);
  327. }
  328.